/***
 * Online Store Retailer
 *
 * Copyright (C) 2013 Integrator Project N.2 Class
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Authors: Leaders: Johan Santacruz, Sebasti�n Santamaria
 *          Quality assurance: Miguel Jimen�z, Juan David Lenis, Johnatan Zarama
 *          A Developers: Sebasti�n Cortes, Luis Dominguez
 *          B Developers: Fabi�n Caicedo, Andr�s Agredo, Javier Vasquez, Nicol�s Lema          
 */
package com.pi2.osr.war.utilities;
import java.util.HashSet;
import java.util.Hashtable;

import com.pi2.osr.ejb.entities.Pagina;

import uk.ac.shef.wit.simmetrics.similaritymetrics.ChapmanMatchingSoundex;


public class MenuOSR {
							   
	private HashSet<Pagina> paginas;
	private Hashtable<String, ModuloMenu> modulos;
	private Hashtable<String, String> items;
	private String[] palabrasReservadas;
	private String _palabrasReservadas;

	public MenuOSR(HashSet<Pagina> paginas, Hashtable<String, String> items, String[] palabrasReservadas){
		this.paginas = paginas; 
		this.items = items;
		this._palabrasReservadas = new String();
		this.modulos = new Hashtable<String, ModuloMenu>();
		
		if(this.items == null || this.items.isEmpty()){
			llenarItems();
		}
		
		if(this.palabrasReservadas == null || this.palabrasReservadas.length == 0){
			llenarPalabrasReservadas();
		}
		
		_palabrasReservadas = getRegexPalabrasReservadas();
		armarModulos();
	}
	
	public void llenarItems(){
		
		items = new Hashtable<String, String>();
		
		items.put("categoria", "Categorías");
		items.put("ciudad", "Ciudades");
		items.put("departamento", "Departamentos");
		items.put("compra", "Compras");
		items.put("estado", "Estados");
		items.put("suministro", "Suministros");
		items.put("producto", "Productos");
		items.put("promocion", "Promociones");
		items.put("proveedor", "Proveedores");
		items.put("tipodocumento", "Tipos de documento");
		items.put("usuario", "Usuarios");
		items.put("cliente", "Clientes");
		items.put("empleado", "Empleados");
		items.put("administrador", "Administradores");
		items.put("ontologia", "Ontología");	
		items.put("osr", "Online Store Retailer");	
	}
	
	public void llenarPalabrasReservadas(){
		
		palabrasReservadas = new String[7];
		
		palabrasReservadas[0] = "\\.xhtml";
		palabrasReservadas[1] = "\\.jsp";
		palabrasReservadas[2] = "\\.html";
		palabrasReservadas[3] = "\\.jsf";
		palabrasReservadas[4] = "osr_backend_war/faces/";
		palabrasReservadas[5] = "osr_frontend_war/faces/";
		palabrasReservadas[6] = "faces/";
	}

	public void armarModulos(){

		ChapmanMatchingSoundex metric = new ChapmanMatchingSoundex();		
		HashSet<Pagina> copia = (HashSet<Pagina>) paginas.clone();

		for (Pagina temporal : paginas) {

			String url = temporal.getUrl().toLowerCase().replaceAll(_palabrasReservadas, "");

			for (String item : items.keySet()) {				
				if(url.indexOf(item) >= 0 ||  metric.getSimilarity(url, item) > 0.9f){		
					agregar(item, items.get(item),  temporal);
					copia.remove(temporal);
					break;
				}
			}
		}

		for (Pagina temporal : copia) {
			agregar("otros", "General", temporal);
		}
		
		items.put("otros", "General");
	}

	public void agregar(String item, String nombreItem, Pagina pagina){
		if(modulos.containsKey(item)){
			modulos.get(item).addPagina(pagina);
		}else{
			ModuloMenu m = new ModuloMenu(item, nombreItem);
			m.addPagina(pagina);
			modulos.put(item, m);
		}
	}
	
	public String getRegexPalabrasReservadas(){
		String regex = "(", pipe = "";
		
		for (String palabra : palabrasReservadas) {
			regex += pipe + palabra;
			pipe = "|";
		}
		
		regex += ")";
		return regex;
	}

	public String capitalize(String line){
	  return Character.toUpperCase(line.charAt(0)) + line.substring(1);
	}

	public HashSet<Pagina> getPaginas() {
		return paginas;
	}

	public Hashtable<String, ModuloMenu> getModulos() {
		return modulos;
	}

	public Hashtable<String, String> getItems() {
		return items;
	}
}